from datetime import datetime, timedelta, time

from odoo import fields, models, _
from odoo.tools import DEFAULT_SERVER_DATETIME_FORMAT
from odoo.tools.misc import format_date


class ReportProductionDailyWizard(models.Model):
    _name = 's.report.production.daily.wizard'
    _description = 'Production Daily Report wizard'

    start_date = fields.Datetime('start date')
    end_date = fields.Datetime('end date')
    work_station_id = fields.Many2one('work.station', string='Work Station')
    product_id = fields.Many2one('product.product', string='Product')

    def action_view(self):
        if self.start_date and self.end_date:
            start_date = min(self.start_date, self.end_date)
            end_date = max(self.start_date, self.end_date)
        else:
            start_date = self.start_date
            end_date = self.end_date

        return {
            'name': _('Production Daily Report'),
            'view_mode': 'tree',
            'res_model': 's.production.daily.report',
            'type': 'ir.actions.act_window',
            'context': {
                'start_date': start_date,
                'end_date': end_date,
                'work_station_id': self.work_station_id.id,
                'product_id': self.product_id.id
            },
            'display_name': f'{format_date(self.env, start_date)} - {format_date(self.env, end_date)}',
            'target': 'current'
        }
